package com.soft.mainsite.module.category.beetl;

import java.util.List;

import com.soft.mainsite.mybatis.model.TCmsCategory;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.soft.mainsite.common.utils.StrUtil;
import com.soft.mainsite.module.category.service.TCmsCategoryService;

/**
 * Description:编辑或者添加栏目，父栏目选择下拉列表树
 *
 * @author liu
 * @create 2019-04-16
 **/
@Service
public class SelectCategoryAutomaticFunction implements Function {

	@Autowired
	private TCmsCategoryService categoryService;

	private final String head = "<option value=\"0\" >顶级节点</option>";

	private final String isSelected = "selected=\"selected\"";

	@Override
	public Object call(Object[] objects, Context context) {

		Long pid = Long.parseLong(objects[0].toString());
		Long currentId = Long.parseLong(objects[1].toString());
		Integer siteId = Integer.parseInt(objects[2].toString());
		if (pid != null) {
			return head + recursion(currentId, pid, 0L, "", siteId);
		}
		return head + recursion(currentId, 0L, 0L, "", siteId);
	}

	/**
	 * 递归输出子节点
	 * @param cid
	 * @param pid
	 * @param sPid
	 * @param tag
	 * @param siteId
	 * @return
	 */
	private String recursion(Long cid, Long pid, Long sPid, String tag, Integer siteId) {
		/* 临时拼凑看不懂就放弃 */
		tag += (StrUtil.isBlank(tag) ? "&nbsp;&nbsp;" : "&nbsp;&nbsp;&nbsp;&nbsp;");
		StringBuffer sbf = new StringBuffer();
		List<TCmsCategory> ctas = categoryService.findCategoryListByPid(sPid, siteId);
		if (ctas != null && ctas.size() > 0) {
			for (TCmsCategory cat : ctas) {

				/* 如果是自己就不输出了 */
				if (cid.intValue() != cat.getCategoryId().intValue() && cid.intValue() != cat.getParentId().intValue()
						|| cid.intValue() == 0) {
					if (ctas.lastIndexOf(cat) == (ctas.size() - 1)) {
						sbf.append(
								"<option value=\"" + cat.getCategoryId() + "\" " + isSelected(cat.getCategoryId(), pid)
										+ ">" + tag + "└" + cat.getCategoryName() + "</option>");
					} else {
						sbf.append(
								"<option value=\"" + cat.getCategoryId() + "\" " + isSelected(cat.getCategoryId(), pid)
										+ ">" + tag + "├" + cat.getCategoryName() + "</option>");
					}
				}
				sbf.append(recursion(cid, pid, cat.getCategoryId(), tag, siteId));
			}
			return sbf.toString();
		}
		return "";
	}

	private String isSelected(Long id, Long perId) {
		if (id.longValue() == perId.longValue()) {
			return isSelected;
		}
		return "";
	}

}
